/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shared;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author eric
 */
public abstract class FileTransferManager extends Thread{

    protected FileInfo fileInfo;
    protected static int MAX_CHUNK_SIZE = 1024;
    private TransferStatus status;
    protected int chunkSize;
    //protected boolean isInProgress;
    protected int successfullChunks;
    protected int totalBytesTransferred;
    protected List<FileTransferListener> listeners;

    public FileTransferManager()
    {
         listeners = new ArrayList();
         status = TransferStatus.IN_PROGRESS;
        //isInProgress= true;
         successfullChunks=0;
         totalBytesTransferred=0;
         successfullChunks = 0;
    }
    public TransferStatus getStatus()
    {
        return status;
    }
    public abstract void run();

    @Override
    public String toString()
    {
            String xfersText = fileInfo.getFileName()
                           + " \t " + getBytesTransferred() + " / " +
                           fileInfo.getFileSize() + " bytes complete" +
                           "\t Status: " + getStatus();
            return xfersText;
    }

        /**
     * @return the bytesRead
     */
    public int getBytesTransferred() {
        return totalBytesTransferred;
    }

    public FileInfo getFileInfo()
    {
        return fileInfo;
    }
/*
    protected void unexpectedTransferTermination()
    {
        for(FileTransferListener l : listeners)
            l.unexpectedTransferTerminationEvent(new BaseEvent (this, "Transfer ended unexpectedly"));
    }

    protected void transferComplete()
    {
        for(FileTransferListener l : listeners)
            l.transferCompleteEvent(this);

 * 
 */
    public void addTransferListener(FileTransferListener l)
    {
        listeners.add(l);
    }

    /**
     * @param status the status to set
     */
    public void setStatus(TransferStatus status) {
        if (this.status != status)
        {
           this.status = status;        
           transferStatusChanged(); 
        }

    }

    private void transferStatusChanged() {
        for(FileTransferListener l : listeners)
            l.transferStatusChanged(this);
    }
}
